package com.mxf.springcloud.controller;


import com.mxf.springcloud.lb.LoadBalancer;
import com.xmf.springcloud.entities.CommonResult;
import com.xmf.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

@RestController
@Slf4j
public class OrderZKController {

    public static final String INVOKE_URL = "http://cloud-provider-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/payment/zk")
    public String paymentInfo() {
        String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
        return result;
    }

    // ========================= 一开始的写法  ===============================

    public static final String LOCALHOST_URL = "http://localhost:8001";

    @GetMapping(value = "/comsumer/payment/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(LOCALHOST_URL + "/payment/" + id, CommonResult.class);
    }

    @PostMapping(value = "/comsumer/payment/create")
    public CommonResult createPayment(@RequestBody Payment payment) {
        return restTemplate.postForObject(LOCALHOST_URL + "/payment/add", payment, CommonResult.class);
    }

    // 加入eureka的写法

    public static final String EUREKA_URL = "http://CLOUD-PAYMENT-SERVICE";

    @GetMapping(value = "/comsumer2/payment/{id}")
    public CommonResult getPaymentById2(@PathVariable("id") Long id) {
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(EUREKA_URL + "/payment/" + id, CommonResult.class);
        if (forEntity.getStatusCode() == HttpStatus.OK) {
            System.out.println("CommonResult：" + (CommonResult)forEntity.getBody());
        }
        return restTemplate.getForObject(EUREKA_URL + "/payment/" + id, CommonResult.class);
    }

    @PostMapping(value = "/comsumer2/payment/create")
    public CommonResult createPayment2(@RequestBody Payment payment) {
        CommonResult forObject = restTemplate.getForObject(EUREKA_URL + "/payment/add", CommonResult.class, payment);
        ResponseEntity<CommonResult> entity = restTemplate.postForEntity(EUREKA_URL + "/payment/add", payment, CommonResult.class);
        if (entity.getStatusCode() == HttpStatus.OK){
            log.info(entity.getBody().toString());
        }
        return restTemplate.postForObject(EUREKA_URL + "/payment/add", payment, CommonResult.class);
    }

    // ========================自定义负载均衡策略==============================
    /*@Autowired
    private LoadBalancer loadBalancer;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping(value = "/consumer/payment/lb/{id}")
    public CommonResult getPaymentLB(@PathVariable("id") Long id){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (instances == null || instances.size() == 0) {
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instances(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "/payment/" + id, CommonResult.class);
    }
*/
}
